// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db=cloud.database()
const _=db.command
// 云函数入口函数
exports.main = async (event, context) => {

  var { openId, record}=event;
  var { consume}=record;
  let proCount = await db.collection('buyRecord').where({
    openid:openId
  }).count();
  if(proCount.total!=1){
    let records=[];
    records.push(record);
    await db.collection('buyRecord').add({
      data:{
        openid: openId,
        records:records,
        sum:consume
      }
    });
  }
  else{
    await db.collection('buyRecord').where({
      openid: openId
    }).update({
      data:{
        records: _.unshift(record),
        sum: _.inc(consume)
      }
    });
  }

  return {
  }
}